<!DOCTYPE html>
<html>
<head>
    <title>发药详情--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/disableHyperlink.css"/>

    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#newNav').click(function () {
                window.location.href = "add.html";
            });
        });

        function checkall() {
            var alls = document.getElementsByName("check");
            var ch = document.getElementById("checkall");
            if (ch.checked) {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = true;
                }
            } else {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = false;
                }
            }
        }

        function delAll() {
            var alls = document.getElementsByName("check");
            var ids = new Array();
            for (var i = 0; i < alls.length; i++) {
                if (alls[i].checked) {
                    ids.push(alls[i].value);
                }
            }
            if (ids.length > 0) {
                if (confirm("确认操作?")) {
                    alert("成功!");
                }
            } else {
                alert("请选中要操作的项");
            }
        }

        $(function () {
            $('#backid').click(function () {
                window.location.href = "dispensing-look.html";
            });
        });
    </script>
</head>
<body>
<form id="searchForm" style="visibility: hidden">
    <input name="peopleId" style="visibility: hidden">
    <input name="index" style="visibility: hidden">
    <input name="size" value="5" style="visibility: hidden">
</form>
<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th>病历号</th>
        <th>姓名</th>
        <th>药品名称</th>
        <th>药品数量</th>
        <th>已发数量</th>
        <th>未发数量</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="hosList"></tbody>
</table>

<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div class="inline pull-right page">
                <a id="firstPage">第一页</a>
                <a id="prePage">上一页</a>
                <span id="pageList">
                </span>
                <a id="nextPage">下一页</a>
                <a id="lastPage">最后一页</a>
                &nbsp;&nbsp;&nbsp;共<span id="total">32</span>条记录<span class='current'> <span
                    id="current_page">1</span>/<span id="total_page">33 </span></span>页
            </div>
        </th>
    </tr>
</table>

</body>
</html>
<script>
    function fillPageData(pageInfo) {
        let peopleId = $("input[name='peopleId']").val();
        let data = "&peopleId=" + peopleId;

        $("#total").html(pageInfo.total);
        $("#total_page").html(pageInfo.pages);
        $("#current_page").html(pageInfo.pageNum);
        $("input[name='index']").val(pageInfo.pageNum);

        $("#firstPage").attr('href', 'dispensing-look.html?index=' + 1 + data);
        $("#prePage").attr('href', 'dispensing-look.html?index=' + (pageInfo.pageNum - 1) + data);
        $("#nextPage").attr('href', 'dispensing-look.html?index=' + (pageInfo.pageNum + 1) + data);
        $("#lastPage").attr('href', 'dispensing-look.html?index=' + (pageInfo.pages) + data);
        $("#firstPage").removeAttr("disabled")
        $("#prePage").removeAttr("disabled")
        $("#nextPage").removeAttr("disabled")
        $("#lastPage").removeAttr("disabled")

        if (pageInfo.isFirstPage) {
            $("#firstPage").attr("disabled", true);
            $("#prePage").attr("disabled", true);
        }

        if (pageInfo.isLastPage) {
            $("#nextPage").attr("disabled", true);
            $("#lastPage").attr("disabled", true);
        }
        let str = "";
        let low = pageInfo.pageNum - 2;
        let high = pageInfo.pageNum + 2;
        if (low >= 1 && high <= pageInfo.pages) {
            for (let i = low; i <= high; i++) {
                str += "<a href='dispensing-look.html?index=" + i + data + "' " + (i == pageInfo.pageNum ? "disabled=true" : "") + ">" + i + "</a>";
            }
        } else if (low < 1) {
            if (pageInfo.pages == 0)
                str += "<a href='dispensing-look.html?index=1" + data + "' disabled=true>1</a>";
            else {
                for (let i = 1; i <= (pageInfo.pages < 5 ? pageInfo.pages : 5); i++) {
                    str += "<a href='dispensing-look.html?index=" + i + data + "' " + (i == pageInfo.pageNum ? "disabled=true" : "") + ">" + i + "</a>";
                }
            }
        } else if (high > pageInfo.pages) {
            for (let i = (pageInfo.pages - 5 >= 1 ? pageInfo.pages - 4 : 1); i <= pageInfo.pages; i++) {
                str += "<a href='dispensing-look.html?index=" + i + data + "' " + (i == pageInfo.pageNum ? "disabled=true" : "") + ">" + i + "</a>";
            }
        }
        $("#pageList").html(str)
    }


    function loadData(index) {
        if (index == null) {
            let url = document.location.toString();
            index = url.GetValue("index");
            if (index == null) {
                index = 1;
            }
            let id = url.GetValue("peopleId");
            if (id != null)
                $("input[name='peopleId']").val(id);
        }
        $("input[name='index']").val(index)
        $.ajax(
            {
                type: "GET",
                data: $("#searchForm").serialize(),
                url: "/medicinePeople/list",
                dataType: "json",
                success: function (vo) {
                    let list = vo.pageInfo.list;
                    let str = "";
                    $("#hosList").html(str);
                    if (vo.code == 200) {
                        for (let i = 0; i < list.length; i++) {
                            let obj = list[i];
                            let remain = obj.drNumber - obj.diNumber;
                            str += "<tr>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.id + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.patient.name + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.drug.name + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.drNumber + "</td>" +
                                "<td style='vertical-align:middle;'>" + obj.diNumber + "</td>" +
                                "<td style='vertical-align:middle;'>" + remain + "</td>" +
                                "<td style='vertical-align:middle;'>" +
                                "<a style='cursor: pointer' onclick='dispense(" + obj.id + "," + remain + ")' " + (remain > 0 ? "" : "disabled") + ">发全</a>" +
                                "  <a style='cursor: pointer' onclick='dispense(" + obj.id + ",1)' " + (remain >= 1 ? "" : "disabled") + ">发1</a>" +
                                "  <a style='cursor: pointer' onclick='dispense(" + obj.id + ",5)' " + (remain >= 5 ? "" : "disabled") + ">发5</a>" +
                                "  <a style='cursor: pointer' onclick='dispense(" + obj.id + ",30)' " + (remain >= 30 ? "" : "disabled") + ">发30</a></td>" + "</td>" +
                                "</tr>";
                        }
                        $("#hosList").html(str)
                        fillPageData(vo.pageInfo);
                    } else {
                        alert("数据加载失败")
                    }
                }
            }
        )
    }

    function dispense(id, number) {
        let peopleId = $("input[name='peopleId']").val();
        let index = $("input[name='index']").val();
        let data = "&peopleId=" + peopleId+"&index=" + index;
        $.ajax({
            type: "POST",
            url: "/medicinePeople/dispense",
            data: "id=" + id + "&number=" + number + "&_method=PUT",
            success: function (vo) {
                if (vo.code == 200) {
                    alert(vo.obj);
                    window.location.href = "dispensing-look.html?"+data;
                } else {
                    alert(vo.msg);
                }
            }
        })
    }

    $(function () {
        loadData();
    })
</script>